package com.tangjia.wangyi;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;
import java.util.zip.Inflater;


public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
    static  class ViewHolder extends RecyclerView.ViewHolder{
        TextView newsString;
        TextView url;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            TextView viewById = itemView.findViewById(R.id.newsString);
            TextView viewById1 = itemView.findViewById(R.id.url);
            this.newsString = viewById;
            this.url=viewById1;
        }
    }
    List<NewsList> src;

    public NewsAdapter(List<NewsList> src) {
        this.src = src;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.newslayout, parent, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        inflate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("android.intent.action.MainActivity2");
                TextView viewById = inflate.findViewById(R.id.url);
                CharSequence text = viewById.getText();
                intent.putExtra("url",text.toString());
                parent.getContext().startActivity(intent);
//                Toast.makeText(parent.getContext(),text,Toast.LENGTH_LONG).show();

            }
        });

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        NewsList newsList = src.get(position);
        String title = newsList.getTitle();
        holder.newsString.setText(title);
        holder.url.setText(newsList.getUrl());
    }

    @Override
    public int getItemCount() {
        return src.size();
    }
}
